package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyAnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeAnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 *  安心-苏州佳云融小红书知乎量
 *  @author 江小游
 */
@ApiPlan(productId = "204", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5AnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang implements ApiOrgPhoneMd5ConvertHandler {

    public static final Integer BRAND=1685;
    public static final String SOURCE="WANH-tf24";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        OrgDataExchangeAnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang orgDataExchange = new OrgDataExchangeAnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang();
        // 手机号
        orgDataExchange.setMd5mobile(apiDataExchangeParameter.getPhoneMd5());
        // 品牌id
        orgDataExchange.setBrand(BRAND);
        return JsonUtils.toJson(orgDataExchange);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        //  撞库返回响应体判断
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getString("code"), "200")) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        //  根据文档响应判断
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        //  进件流程
        OrgApplyAnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang orgApply = new OrgApplyAnXinSuZhouJiaYunRongXiaoHongShuZhiHuLiang();
        //  赋值进件属性
        orgApply.setSource(SOURCE);
        orgApply.setName(apiApplyParameter.getUserName());
        // 手机号
        orgApply.setMobile(apiApplyParameter.getPhone());
        //  获取封装字段转换器
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  获取年龄
        orgApply.setAge(apiFieldConverter.getAgeInt());
        // 城市
        orgApply.setCity(apiApplyParameter.getCity().replace("市",""));
        /*
            贷款金额
           1.3万以下
           2.5万-10万
           3.10万-20万
         */
        String loanAmount = ChooseEq.create(apiApplyParameter.getLoanAmount())
                .when("1", "3")
                .when("2", "10")
                .when("3", "20")
                .when("4", "40")
                .end();
        orgApply.setMoney(loanAmount);
        //  性别-男/女
        orgApply.setSex(apiFieldConverter.getSexText());
        // 房 有/无
        orgApply.setHouse(apiFieldConverter.hasRealEstateInfoToStr());
        // 车 有/无
        orgApply.setCar(apiFieldConverter.hasCarInfoToStr());
        // 社保有/无
        orgApply.setShebao(apiFieldConverter.hasSocialSecurityToStr());
        // 公积金 有/无
        orgApply.setGongjijin(apiFieldConverter.hasProvidentFundToStr());
        //  工作 有/无
        orgApply.setJob(apiFieldConverter.hasCareerToStr());
        // 保险保单 有/无
        orgApply.setBaodan_is(apiFieldConverter.hasInsurancePolicyToStr());
        // 信用卡 有/无
        orgApply.setCredit_card(apiFieldConverter.hasCreditLimitToStr());
        // 银行代发-有/无
        orgApply.setIsbankpay(apiFieldConverter.hasSalaryBankToStr());
        // 芝麻分 650，700
        orgApply.setZhima(apiFieldConverter.getSesameCreditNumber());
        // 备注
        orgApply.setSource_remark(apiApplyParameter.getFormRemark());
        // 时间搓
        orgApply.setTime((int)(System.currentTimeMillis()/1000L));
        return orgApply;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        //  进件返回响应体判断
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        //  根据文档响应判断
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
